package sys_ctl

import (
	"net/http"
	"ubt-rbac-service/internal/app/model"
	"ubt-rbac-service/internal/app/model/sys_mod"
	"ubt-rbac-service/internal/app/service/sys_serv"
	"ubt-rbac-service/internal/app/util"
	"ubt-rbac-service/internal/core/convert"
)

type sysLog struct{}

var SysLogCtl sysLog

// Page 获取日志树状列表
func (c *sysLog) Page(w http.ResponseWriter, r *http.Request) {
	// 通过头部请求的token获取用户信息
	token := r.Header.Get("token")
	userInfo := util.GetUserInfo(token)
	if userInfo == nil {
		util.ToJson(w, &model.ResJson{Code: 401, Msg: "对不起，您未登录或登录超时，请登录！"})
		return
	}
	u := r.URL.Query()
	if u == nil {
		util.ToJson(w, &model.ResJson{Code: 400, Msg: "对不起，请求的参数不能为空，请重试"})
		return
	}
	userId := u.Get("userId")
	modCode := u.Get("modCode")
	kinds := u.Get("kind")
	begins := u.Get("beginDate")
	ends := u.Get("endDate")
	limits := u.Get("limit")
	offsets := u.Get("offset")
	userId = util.InputFilter(userId, true)
	modCode = util.InputFilter(modCode, true)
	kinds = util.InputFilter(kinds, true)
	begins = util.InputFilter(begins, true)
	ends = util.InputFilter(ends, true)
	limits = util.InputFilter(limits, true)
	offsets = util.InputFilter(offsets, true)
	kind := convert.Str2u8(kinds)
	beginDate := convert.Str2int(begins)
	endDate := convert.Str2int(ends)
	limit := convert.Str2u8(limits)
	offset := convert.Str2u32(offsets)
	// 先统计信息的数量
	req := &sys_mod.SysLogPageReq{UserId: userId, ModCode: modCode, Kind: kind, BeginDate: beginDate, EndDate: endDate, OptOrg: userInfo.DeptId}
	if result := sys_serv.SysLogServ.Count(req); result != nil && result.RetCode == 1001 && result.RetData.(int64) > 0 {
		res := &sys_mod.SysLogPageRes{Total: result.RetData.(int64)}
		// 再查询具体的信息列表
		req.Limit = limit
		req.Offset = offset
		if result = sys_serv.SysLogServ.Page(req); result != nil && result.RetCode == 1001 {
			res.List = result.RetData
			util.ToJson(w, &model.ResJson{Code: 200, Msg: "ok", Data: res})
			return
		}
	}
	util.ToJson(w, &model.ResJson{Code: 404, Msg: "暂无内容"})
}
